<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/10
 * Time: 10:08
 */
namespace app\admin\controller;

use think\Db;

class Category extends Base{

    /**
     * 类目列表
     */
    public function cate_list(){
        //获取管理员列表数据
        $admin_list = Db::table('yf_items_cate')->paginate(20);
        // 获取分页显示
        $page = $admin_list->render();
        // 模板变量赋值
        $this->assign('list', $admin_list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }


    /**
     * 后台类目编辑
     */
    public function cate_edit(){

        if ($this->request->isPost()){
            $name = input('name','');
            $id = input('id','');
            $status = input('status',1);
            $sort = input('sort','');
            if ($name == ''){
                return json(['code'=>0,'msg'=>'类名未填']);
            }
            if ($id == ''){
                return json(['code'=>0,'msg'=>'参数错误']);
            }
            $data['name'] = $name;//类名
            if ($sort !=''){
                $data['sort'] = $sort; //排序
            }
            $data['status'] = $status; //状态
            $data['id'] = $id;
            $rst = Db::name('items_cate')->update($data);
            if ($rst != false){
                return json(['code'=>200,'msg'=>'权限修改成功']);
            }else{
                return json(['code'=>0,'msg'=>'权限修改失败']);
            }
        }else{
            //类目信息
            $cate_info = Db::table('yf_items_cate')->where(['id'=>input('id')])->find();
            $this->assign('cate_info',$cate_info);
            return $this->fetch();
        }
    }

    /***
     * 类目排序
     */
    public function cate_order(){
        if (!$this->request->isPost()){
            return json(['code'=>0,'msg'=>'提交方式不正确']);
        }else{
            foreach ($_POST as $id => $sort){
                Db::name('items_cate')->where(array('id' => $id ))->setField('sort' , $sort);
            }
            return json(['code'=>200,'msg'=>'排序更新成功']);
        }
    }





    /**
     * 更改管理员状态
     */
    public function change_admin()
    {
        if (request()->isAjax()){
            $admin_id = input('param.admin_id',0);
            if ($admin_id == 0 || $admin_id == 1){
                return json(['code'=>0,'msg'=>'管理员传入有误']);
            }else{
                $admin_open = db('admin')->where('id',$admin_id)->value('admin_open');
                if ($admin_open == 0){
                    db('admin')->where('id',$admin_id)->setField(['admin_open'=>1]);
                }else{
                    db('admin')->where('id',$admin_id)->setField(['admin_open'=>0]);
                }
            }
            return json(['code'=>200,'msg'=>'管理员状态设置成功']);
        }

    }

    /**
     * 空操作
     */
    public function _empty(){
        if($this->check_admin_login()){
            $this->redirect('admin/Index/index');
        }else{
            $this->redirect('admin/login');
        }
    }

}